package com.piano.moguyun.utils;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.piano.moguyun.MyApplication;
import com.piano.moguyun.R;


/**
 * 提示工具
 */
public class ToastUtils {

    private static String oldMsg;
    private static long oldMsgTime = 0;
    private static final long MIN_INTERVAL_TIME = 1000; // 最小间隔时间

    public static void toast(String msg,int type){
        if(msg == null){
            return;
        }

        if(msg.equals(oldMsg)){ // 如果发送消息内容和上次一样
            if(System.currentTimeMillis() - oldMsgTime < MIN_INTERVAL_TIME){  // 小于最小间隔时间
                return;
            }
        }
        oldMsg = msg;
        oldMsgTime = System.currentTimeMillis();
        toast(type);
    }


    /**
     * 提示框
     * @param type 类型 1:成功 2:错误 3:提示
     */
    public static void toast(int type) {
        // 自定义布局
        View view = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.toast_utils, null);
        TextView tvContent = view.findViewById(R.id.tvContent);
        ImageView ivToastImg = view.findViewById(R.id.ivToastImg);
        if (type == 1){
            ivToastImg.setImageResource(R.mipmap.icon_toast_success);
        }else if (type == 2){
            ivToastImg.setImageResource(R.mipmap.icon_toast_fail);
        }else {
            ivToastImg.setImageResource(R.mipmap.icon_toast_hint);
        }
        tvContent.setText(oldMsg);

        Toast toast = new Toast(MyApplication.getInstance());
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0 , 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

}
